﻿
Imports xmlctrl

Public MustInherit Class controlProcessor
    Inherits requestProcessor

    Protected MustOverride Function processImpl(ByVal context As httpContext, _
                                                ByRef control As xmlcontrol) As Boolean
    Public Delegate Function processImplDelegate(ByVal context As httpContext, _
                                                 ByRef control As xmlcontrol) As Boolean

    Public Shared Function process2(ByVal context As httpContext, ByVal pimpl As processImplDelegate, _
                                    ByVal enableEmptyControl As Boolean) As Boolean
        assert(Not context Is Nothing, "context is nothing.")
        assert(Not pimpl Is Nothing, "processImplDelegate is nothing.")

        Dim control As xmlcontrol = Nothing
        Dim rtn As Boolean = False
        If pimpl(context, control) Then
            If Not control Is Nothing Then
                If Not control.render() Then
                    raiseError("cannot render xmlcontrol, ignore request.", errorHandle.errorType.warning)
                    rtn = False
                Else
                    control.clear()
                    rtn = True
                End If
            ElseIf context.response().StatusCode = Net.HttpStatusCode.OK _
                AndAlso Not enableEmptyControl _
                AndAlso context.buff().Position = 0 Then
                raiseError("return empty xmlcontrol for request " + context.url().full() + ", ignore request." _
                           , errorHandle.errorType.warning)
                rtn = False
            Else
                'if the processor need no control to some special requests and 
                'the statuscode is not ok or already write context.buff,
                'should directly return without setstatuscode and setcontentEncoding/setcontentType
                Return True
            End If
        Else
            raiseError("cannot process context for request " + context.url().full() + ", ignore request." _
                        , errorHandle.errorType.warning)
            rtn = False
        End If

        On Error Resume Next
        If rtn Then
            context.setstatuscode(Net.HttpStatusCode.OK, "OK")
            If context.response.ContentEncoding Is Nothing Then
                context.setcontentEncoding(encoding.utf8)
            End If
            If isemptystring(context.response.ContentType) Then
                context.setcontentType(network.http.contentType.texthtmlUtf8)
            End If
        End If

        Return rtn
    End Function

    Protected Overridable Function enableEmptyControl(ByVal context As httpContext) As Boolean
        Return enableEmptyControl()
    End Function

    Protected Overridable Function enableEmptyControl() As Boolean
        Return False
    End Function

    Public NotOverridable Overrides Function process(ByVal context As httpContext) As Boolean
        Return process2(context, AddressOf processImpl, enableEmptyControl())
    End Function
End Class
